package com.bc004346.travelus.Adapter;

import android.app.Activity;
import android.graphics.Typeface;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bc004346.travelus.Model.Domain.Flight;
import com.bc004346.travelus.Model.Interface.IFlightAvailabilityRequestListener;
import com.bc004346.travelus.Model.Interface.ISelectFlightListener;
import com.bc004346.travelus.R;

import java.util.ArrayList;

import static com.bc004346.travelus.Utility.Utility.DAY_DATE;
import static com.bc004346.travelus.Utility.Utility.MILITARY_TIME;
import static com.bc004346.travelus.Utility.Utility.log;

/**
 * Created by Renats on 9/5/2014.
 * Generic * Flight adapter
 */
public class FlightAdapterWithAvailability extends ArrayAdapter<Flight> {

    private Activity mActivity;
    private CompoundButton mButton;
    private TextView mFirstCapacity;
    private TextView mCoachCapacity;
    private TextView mFirstStandby;
    private TextView mCoachStandby;

    public FlightAdapterWithAvailability(Activity activity, ArrayList<Flight> flights) {
        super(activity, 0, flights);
        mActivity = activity;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        try {
            if (convertView == null)
                convertView = mActivity.getLayoutInflater()
                        .inflate(R.layout.list_item_flight_with_availability, null);

            final Flight f = getItem(position);
            f.setFlightPosition(position);
            Typeface font = Typeface.createFromAsset(mActivity.getAssets(), "arial.ttf");

            final ImageView arrow = (ImageView) convertView.findViewById(R.id.list_item_flight_details_arrow);
            TextView flightNumber = (TextView) convertView.findViewById(R.id.list_item_flight_flight_number);
            TextView departureDate = (TextView) convertView.findViewById(R.id.list_item_flight_departure_date);
            TextView originDestination = (TextView) convertView.findViewById(R.id.list_item_flight_origin_destination);
            TextView firstSA = (TextView) convertView.findViewById(R.id.list_item_flight_first_sa);
            TextView coachSA = (TextView) convertView.findViewById(R.id.list_item_flight_coach_sa);
            mFirstCapacity = (TextView) convertView.findViewById(R.id.list_item_flight_first_capacity);
            mCoachCapacity = (TextView) convertView.findViewById(R.id.list_item_flight_coach_capacity);
            mFirstStandby = (TextView) convertView.findViewById(R.id.list_item_flight_first_standby);
            mCoachStandby = (TextView) convertView.findViewById(R.id.list_item_flight_coach_standby);
            final LinearLayout capacity = (LinearLayout) convertView.findViewById(R.id.list_item_flight_capacity);
            final LinearLayout standby = (LinearLayout) convertView.findViewById(R.id.list_item_flight_standby);
            LinearLayout availability = (LinearLayout) convertView.findViewById(R.id.list_item_flight_availability);
            final CheckBox isSelected = (CheckBox) convertView.findViewById(R.id.list_item_flight_check_box);

            flightNumber.setText(String.format("Flight %s", String.valueOf(f.getFlightNumber())));
            departureDate.setText(String.format("%s %s",
                    MILITARY_TIME.format(f.getDeparture()),
                    DAY_DATE.format(f.getDeparture())));
            originDestination.setText(String.format("%s > %s",
                    f.getOrigin(),
                    f.getDestination()));
            firstSA.setText(String.valueOf(f.getFirstClass().getSeatsAvailable()));
            coachSA.setText(String.valueOf(f.getCoach().getSeatsAvailable()));
            mFirstCapacity.setText(String.valueOf(f.getFirstClass().getCapacity()));
            mCoachCapacity.setText(String.valueOf(f.getCoach().getCapacity()));
            mFirstStandby.setText(String.valueOf(f.getFirstClass().getNonRevsCount()));
            mCoachStandby.setText(String.valueOf(f.getCoach().getNonRevsCount()));

            departureDate.setTypeface(font);
            originDestination.setTypeface(font);
            firstSA.setTypeface(font);
            flightNumber.setTypeface(font);
            firstSA.setTypeface(font);

            isSelected.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                    mButton = buttonView;
                    f.setSelected(isChecked);
                    ((ISelectFlightListener) mActivity).selectFlight(f);
                }
            });

            availability.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (capacity.getVisibility() == View.VISIBLE) {
                        capacity.setVisibility(View.GONE);
                        standby.setVisibility(View.GONE);
                        arrow.setRotation(90);
                    } else {
                        ((IFlightAvailabilityRequestListener) mActivity).updateFlightAvailability(f);
                        capacity.setVisibility(View.VISIBLE);
                        standby.setVisibility(View.VISIBLE);
                        arrow.setRotation(270);
                        mFirstCapacity = (TextView) v.findViewById(R.id.list_item_flight_first_capacity);
                        mCoachCapacity = (TextView) v.findViewById(R.id.list_item_flight_coach_capacity);
                        mFirstStandby = (TextView) v.findViewById(R.id.list_item_flight_first_standby);
                        mCoachStandby = (TextView) v.findViewById(R.id.list_item_flight_coach_standby);
                    }
                }
            });
        } catch (Exception e) {
            log(e.getMessage());
        }

        return convertView;
    }

    public void deselectFlight(Flight flight) {
        flight.setSelected(!flight.isSelected());
        mButton.setChecked(!mButton.isChecked());
    }

    public void updateFlight(Flight flight) {
        mFirstCapacity.setText(String.valueOf(flight.getFirstClass().getCapacity()));
        mCoachCapacity.setText(String.valueOf(flight.getCoach().getCapacity()));
        mFirstStandby.setText(String.valueOf(flight.getFirstClass().getNonRevsCount()));
        mCoachStandby.setText(String.valueOf(flight.getCoach().getNonRevsCount()));
    }
}
